package com.github.llyb120.performance.define;

import groovy.lang.Closure;
import java.util.*;
import static com.github.llyb120.performance.define.Helper.*;
import static com.github.llyb120.performance.define.Helper.EventType.POST;
import static com.github.llyb120.performance.define.Helper.EventType.PRE;


public class All {


    private final static ThreadLocal<LinkedList> local = new ThreadLocal<LinkedList>(){
        @Override
        protected LinkedList initialValue() {
            return new LinkedList();
        }
    };
    private final static ThreadLocal<LinkedList<String>> localPath = new ThreadLocal(){
        @Override
        protected LinkedList initialValue() {
            return new LinkedList();
        }
    };

    public static void generator (Closure generator){
        pushPathStack("generator");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Generator _generator = new Generator();

        pushStack(_generator);
        if(generator != null){
            generator .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitGenerator(new Context(localPath.get(), local.get()));
        }
        localElement.get().add(_generator);
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void handler (Closure handler){
        pushPathStack("handler");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Generator) {
            ((Generator)item).setHandler (handler);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void name (String name){
        pushPathStack("name");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Generator) {
            ((Generator)item).setName (name);
        } else if( item instanceof Datasource) {
            ((Datasource)item).setName (name);
        } else if( item instanceof Dataset) {
            ((Dataset)item).setName (name);
        } else if( item instanceof Converter) {
            ((Converter)item).setName (name);
        } else if( item instanceof Quota) {
            ((Quota)item).setName (name);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void datasource (Closure datasource){
        pushPathStack("datasource");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Datasource _datasource = new Datasource();

        pushStack(_datasource);
        if(datasource != null){
            datasource .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitDatasource(new Context(localPath.get(), local.get()));
        }
        localElement.get().add(_datasource);
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void password (String password){
        pushPathStack("password");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Datasource) {
            ((Datasource)item).setPassword (password);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void driver (String driver){
        pushPathStack("driver");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Datasource) {
            ((Datasource)item).setDriver (driver);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void url (String url){
        pushPathStack("url");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Datasource) {
            ((Datasource)item).setUrl (url);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void username (String username){
        pushPathStack("username");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Datasource) {
            ((Datasource)item).setUsername (username);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void dataset (Closure dataset){
        pushPathStack("dataset");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Dataset _dataset = new Dataset();

        pushStack(_dataset);
        if(dataset != null){
            dataset .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitDataset(new Context(localPath.get(), local.get()));
        }
        localElement.get().add(_dataset);
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void field (Closure field){
        pushPathStack("field");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Field _field = new Field();
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Dataset) {
            ((Dataset)item).getField ().add( _field);
        }

        pushStack(_field);
        if(field != null){
            field .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitField(new Context(localPath.get(), local.get()));
        }
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void scope (Closure scope){
        pushPathStack("scope");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Scope _scope = new Scope();
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Dataset) {
            ((Dataset)item).setScope ( _scope);
        }

        pushStack(_scope);
        if(scope != null){
            scope .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitScope(new Context(localPath.get(), local.get()));
        }
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void lead (Closure lead){
        pushPathStack("lead");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Lead _lead = new Lead();
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Dataset) {
            ((Dataset)item).setLead ( _lead);
        }

        pushStack(_lead);
        if(lead != null){
            lead .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitLead(new Context(localPath.get(), local.get()));
        }
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void use (String use){
        pushPathStack("use");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Lead) {
            ((Lead)item).setUse (use);
        } else if( item instanceof Scope) {
            ((Scope)item).setUse (use);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void table (String table){
        pushPathStack("table");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Lead) {
            ((Lead)item).setTable (table);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void field (String en,String cn,String converter){
        field (en,cn,converter,(Closure) null );
    }
    public static void field (String en,String cn,String converter,Closure field){
        pushPathStack("field");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Field _field = new Field();
        _field .setEn (en);
        _field .setCn (cn);
        _field .setConverter (converter);
        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Dataset) {
            ((Dataset)item).getField ().add( _field);
        }

        pushStack(_field);
        if(field != null){
            field .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitField(new Context(localPath.get(), local.get()));
        }
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void converter (String converter){
        pushPathStack("converter");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Field) {
            ((Field)item).setConverter (converter);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void en (String en){
        pushPathStack("en");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Field) {
            ((Field)item).setEn (en);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void cn (String cn){
        pushPathStack("cn");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Field) {
            ((Field)item).setCn (cn);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void converter (Closure converter){
        pushPathStack("converter");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Converter _converter = new Converter();

        pushStack(_converter);
        if(converter != null){
            converter .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitConverter(new Context(localPath.get(), local.get()));
        }
        localElement.get().add(_converter);
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void output (Closure output){
        pushPathStack("output");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Converter) {
            ((Converter)item).setOutput (output);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void input (Closure input){
        pushPathStack("input");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Converter) {
            ((Converter)item).setInput (input);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void sql (String sql){
        pushPathStack("sql");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Scope) {
            ((Scope)item).setSql (sql);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void group (String[] group){
        pushPathStack("group");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Scope) {
            ((Scope)item).setGroup (group);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void quota (Closure quota){
        pushPathStack("quota");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Quota _quota = new Quota();

        pushStack(_quota);
        if(quota != null){
            quota .call();
        }
        //visit node
        for(Interceptor interceptor : Helper.interceptors){
            interceptor.visitQuota(new Context(localPath.get(), local.get()));
        }
        localElement.get().add(_quota);
        popStack();

        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }
    public static void calculate (Closure calculate){
        pushPathStack("calculate");

        //pre event begin
        //String __path = getCurrentPath();
        //Context __context = new Context(__path, local.get());
        //Emit(PRE, __path, __context);
        //pre event end

        Object item = pickStack();
        if(false){
            //generated nothing
        } else if( item instanceof Quota) {
            ((Quota)item).setCalculate (calculate);
        }


        //post event begin
        //Emit(POST, __path, __context);
        //post event end

        popPathStack();

    }



    private static Object pickStack(){
        return local.get().getLast();
    }
    private static void pushStack(Object obj){
        local.get().addLast(obj);
    }
    private static Object popStack(){
        return local.get().removeLast();
    }
    private static void pushPathStack(String name){
        localPath.get().addLast((name));
    }
    private static Object popPathStack(){
        return localPath.get().removeLast();
    }
    private static String getCurrentPath(){
        return String.join(".", localPath.get());
    }

}